﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static internal Thread[] threads = new Thread[10];
        static void Main(string[] args)
        {
            //DisplayValue(); //这里不会阻塞  
            //Console.WriteLine("MyClass() End.");
            //Console.ReadKey();
            //Func<string,string,string> a = (x,y) => { return x+y; };            
            //var b=a("a","b");
            //Action<string> c = (x) => { Console.WriteLine(x); };
            //c("cccc");

            Console.Title = "Test";
            Thread.CurrentThread.Name = "System Thread";//给当前线程起名为"System Thread"
            Console.WriteLine(Thread.CurrentThread.Name + "'Status:" + Thread.CurrentThread.ThreadState);

            Console.WriteLine("Thread Start/Stop/Join Sample");

            #region  简易多线程实例
            //Alpha oAlpha = new Alpha();
            ////file://这里创建一个线程，使之执行Alpha类的Beta()方法
            //Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
            //oThread.Start();
            //while (!oThread.IsAlive) ;
            //Thread.Sleep(1);
            //oThread.Abort();
            //oThread.Join();
            //Console.WriteLine();
            //Console.WriteLine("Alpha.Beta has finished");
            //try
            //{
            //    Console.WriteLine("Try to restart the Alpha.Beta thread");
            //    oThread.Start();
            //}
            //catch (ThreadStateException)
            //{
            //    Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
            //    Console.WriteLine("Expected since aborted threads cannot be restarted.");
            //    Console.ReadLine();
            //}
            #endregion

            Account acc = new Account(0);
            for (int i = 0; i < 10; i++)
            {
                Thread t = new Thread(new ThreadStart(acc.DoTransactions));
                threads[i] = t;
            }
            for (int i = 0; i < 10; i++)
                threads[i].Name = i.ToString();
            for (int i = 0; i < 10; i++)
                threads[i].Start();
            Console.ReadLine();

        }


       
    
  
        public static Task<double> GetValueAsync(double num1, double num2)
        {
            return Task.Run(() =>
            {
                for (int i = 0; i < 1000000; i++)
                {
                    num1 = num1 / num2;
                }
                return num1;
            });
        }
        public static async void DisplayValue()
        {
            double result = await GetValueAsync(1234.5, 1.01);//此处会开新线程处理GetValueAsync任务，然后方法马上返回  
            //这之后的所有代码都会被封装成委托，在GetValueAsync任务完成时调用  
            Console.WriteLine("Value is : " + result);
        }  
    }
}
